import { observable, action, computed } from "mobx";
import { Tools, Bubble } from "weaver-mobile";
import { toJS } from "mobx";
import { Browser } from 'weaver-mobile-page';
import * as api from "../apis/sendBless.js";
const getLabel = Tools.getLabel;

export class HrmSendBlessStore {
    prefixUrl = "/hrm/hrm_e9/image/birth/";
    greetingCardUrlSuffixs = ["1", "2", "3", "4", "5", "6", "7", "8", "9"];
    greetingCardUrl = this.greetingCardUrlSuffixs.map(suffix => `${this.prefixUrl}${suffix}.png`);

    @observable startIndex = 0;
    @observable tagGroupDatas = [];
    @observable blessing = "";
    @observable browserConditionParam = {};
    @observable checkedImg = "/hrm/hrm_e9/image/birth/1.png";
    @observable birthTargets = [];
    @observable targetId = "";
    @observable browserStore = new Browser.Store({ route: '/birthdayRemind/sendBless' });


    @computed get defaultBless() {
        return getLabel(528863, "Happy Birthday！生日快乐！愿心想事成，天天开心！")
    }

    @computed get greetingCardShow() {
        const index = this.startIndex;

        return this.greetingCardUrl.slice(index * 2, index*2 + 2).map(url => (this.checkedImg == url) ? ({ url, isChecked: true }) : ({ url, isChecked: false }));
    }

    @computed get canPrev() {
        return this.startIndex > 0;
    }

    @computed get canNext() {
        const gclen = this.greetingCardUrlSuffixs.length;
        return this.startIndex < Math.floor(gclen / 2);
    }

    @computed get blessOptions() {
        return this.birthTargets.filter(target => target.id != this.targetId);
    }

    @computed get birthIds() {
        return this.birthTargets.map(data => data.id);
    }

    @action prev = () => {
        this.canPrev && this.startIndex--;
    }

    @action next = () => {
        this.canNext && this.startIndex++;
    }

    @action getBlessingForm = () => {
        const params = { targetId: this.targetId, createDate: this.createDate,recordId:this.recordId };
        api.getBlessingForm(params).then(res => {
            ["blessing", "browserConditionParam"].forEach(v => {
                if (res[v]) {
                    this[v] = res[v];
                }
            })
            if (window.e9_locale.userLanguage != '7') {
                this.blessing = getLabel('529463','生日快乐！愿心想事成，天天开心！')
            }
            const { browserConditionParam } = res;
            if (browserConditionParam) {
                this.tagGroupDatas = browserConditionParam.replaceDatas
            }
        })
    }

    sendBless = () => {
        const validDatas = this.tagGroupDatas.filter(unhandledData => this.birthTargets.find(validData => unhandledData.id == validData.id));

        if (!validDatas.length) {
            Bubble.warning(getLabel(528864, "请至少选择1个祝福对象！"));
            return;
        }

        const params = {
            imgUrl: this.checkedImg,
            target: validDatas.map(data => data.id).join(","),
            blessing: this.blessing ? this.blessing : this.defaultBless
        };

        api.saveBlessingForm(params).then(res => {
            if (res.status == "1") {
                Bubble.success(getLabel(528865, `成功发送{param}个祝福！`).replace("{param}", validDatas.length));
                this.history && this.history.go(-1);
            } else {
                Bubble.error(res.message);
            }
        })
    }


    @action handleImgClick = (srcName) => {
        this.checkedImg = srcName
    }

    @action handleTagGroupChange = (value, valueSpan, valueArray) => {
        this.tagGroupDatas = valueArray;
    }

    @action addBlessTarget = (data) => {
        if (!this.tagGroupDatas.find(oldData => oldData.id == data.id)) {
            this.tagGroupDatas.push(data);
        }
    }

    @action checkAll = () => {
        this.tagGroupDatas = this.birthTargets;
    }

    handleTextareaChange = (val) => {
        this.blessing = val;
    }

    setPropsData = (datas) => {
        Object.keys(datas).forEach(key => {
            this[key] = datas[key];
        })
    }


}